var cusUnique;//用户唯一性标识
var cusFlag;//客户身份 0-注册用户  1-游客

$(document).ready(function() {
	cusUnique=getCookie("cusUnique");
	
	if(cusUnique!=null){
		loadCusInfo(cusUnique);
	}
	
});




//加载用户基本信息
function loadCusInfo(cusUnique) {
	$.ajax({
		url : "../cus/findCusById.do",
		type : "post",
		data : {
			"cusUnique" : cusUnique
		},
		dataType : "json",
		success : function(result) {
			console.log(result);
			if (result.status == 1) {// 查询成功
				$("#cusAccount").text(result.data.cusAccount);
			} else {
				$("#cusAccount").text("游客");
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		}
	});
}










//修改密码提交
$("#changeSubmit").click(function(){
	if(cusUnique==null){
		return;
	}
	var oldPassword=$("#pold_password").val();
	var newPassword=$("#pnew_password").val();
	var confirmPassword=$("#pcomfirm_password").val();
	console.log("cusId:"+cusUnique+";oldPassword:"+oldPassword+";newPassword:"+newPassword+";confirmPassword:"+confirmPassword);
	var ok=true;
	if(oldPassword==""){
		$("#pold_password").focus();
		ok=false;
		return;
		
	}
	if(newPassword==""){
		$("#pnew_password").focus();
		ok=false;
		return;
	}
	
	if(confirmPassword==""){
		$("#pcomfirm_password").focus();
		ok=false;
		return;
	}
	if(confirmPassword!=newPassword){
		$("#password_msg").text("两次密码不一致");
		setTimeout(function(){$('#password_msg').text("");}, 2000);
		$('#pnew_password').val('');
		$("#pcomfirm_password").val("");
		$("#pcomfirm_password").focus();
		ok=false;
		return;
	}
	
	if(oldPassword==newPassword){
		$("#password_msg").text("新旧密码不能一样");
		setTimeout(function(){$('#password_msg').text("");}, 2000);
		$("#pold_password").val('');
		$('#pnew_password').val('');
		
		$("#pnew_password").focus();
		ok=false;
		return;
	}
	if(ok){
		$.ajax({
			url : "../cus/findRWByCusId.do",
			type : "post",
			traditional: true,
			data : {"cusId":cusUnique,"cusPwd":oldPassword},
			dataType : "json",
			success : function(result) {
				console.log(1111);
				if (result.status == 1) {// 查询成功
					$.ajax({
						url : "../cus/updatePassWord.do",
						type : "post",
						traditional: true,
						data : {"cusId":cusUnique,"cusPwd":newPassword},
						dataType : "json",
						success : function(result) {
							if (result.status == 1) {// 查询成功
								$('#alert-finish').text('修改成功！');
								$('#alert-finish').slideToggle(500);
								window.location.href="center.html";
							} else {
								$("#password_msg").text("网络异常");
								setTimeout(function(){$('#password_msg').text("");}, 2000);
								$("#pold_password").focus();
							}
						},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
						}
					});
					
					
				} else {
					$("#password_msg").text("旧密码不正确");
					setTimeout(function(){$('#password_msg').text("");}, 2000);
					$("#pold_password").focus();
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}
	
});



//添加新地址
function goAddAddress(){
	window.location.href="add-address.html?cusUnique="+cusUnique+"&dest="+2;
	
}
